package com.milorad.ads.pages.admin;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.milorad.ads.data.dao.interfaces.CityDao;
import com.milorad.ads.data.domain.City;
import com.milorad.ads.pages.BasePage;

/**
 * Cities administration page.
 * 
 * @author Milorad
 */
public class CitiesAdministrationPage extends BasePage {
	
	@SpringBean
	private CityDao cityDao;
	
	public CitiesAdministrationPage() {				
		
		// new city link
		add(createNewCityLink());
		
		// list with all cities, and change option 
		add(createCitiesList());
		
		add (new BookmarkablePageLink<Void>("adminPanelLink", MainAdminPage.class));
	}

	private Link<String> createNewCityLink() {
		
		return new Link<String>("newCityLink") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(new CreateEditCityPage(new City()));				
			}			
		};
	}

	private ListView<City> createCitiesList() {
		
		return new ListView<City>("citiesList", cityDao.findAll()) {

			private static final long serialVersionUID = -4083520522187567842L;

			@Override
			protected void populateItem(ListItem<City> item) {
				City city = item.getModelObject();
				item.add(new Label("id", city.getId().toString()));
				item.add(new Label("name", city.getName()));
					
				item.add(new Link<City>("change", item.getModel()) {
					
					private static final long serialVersionUID = 2221207599260261693L;
					
					@Override
					public void onClick() {
						City city = getModelObject();
						setResponsePage(new CreateEditCityPage(city));
						
					}					
				});
			}						
		};
	}
}